<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="5">
        <el-button type="primary" size="mini" @click="handleAdd">添加</el-button>
      </el-row>
      <el-row :gutter="5">
        <el-divider content-position="left">膳食食谱-<span style="color: #999; font-size: 10px;">食谱每周更新</span></el-divider>
      </el-row>
      <div style="margin-left:1%;margin-right:1%">
        <el-row>
            <el-col :span="3" v-for="(item) in planList" :key="item.planId" :offset="0" >
              <div class="card-container">
                <el-card :body-style="{ padding: '0px'}" shadow="hover" class="firstCard">
                  <div class="e-info">
                    <el-card class="mealPlan" body-style="{ padding: '0px'}" shadow="hover" >
                      <el-tag size="mini" type="success">早餐:</el-tag>{{item.breakFast}}<br>
                      <el-tag size="mini" type="warning">午餐:</el-tag>{{item.noonFast}}<br>
                      <el-tag size="mini" type="danger">下午茶:</el-tag>{{item.afternoonTea}}<br>
                      <el-tag size="mini" >晚餐:</el-tag>{{item.dinner}}
                    </el-card>
                    <div class="bottom clearfix">
                      <time class="time">日期:<el-tag type="success" size="mini">{{item.planTime}}</el-tag></time>
                      <el-button type="warning" icon="el-icon-edit" plain class="button" size="mini" @click="handleUpdate(item.planId)"></el-button>
                    </div>
                  </div>
                </el-card>
              </div>
            </el-col>

        </el-row>
        <pagination
          v-show="total>=0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="早餐" prop="breakFast">
            <el-input v-model="form.breakFast" placeholder="请输入早餐" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}" maxlength="250"/>
          </el-form-item>
          <el-form-item label="午餐" prop="noonFast">
            <el-input v-model="form.noonFast" placeholder="请输入午餐" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}" maxlength="250"/>
          </el-form-item>
          <el-form-item label="下午茶" prop="afternoonTea">
            <el-input v-model="form.afternoonTea" placeholder="请输入下午茶" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}" maxlength="250"/>
          </el-form-item>
          <el-form-item label="晚餐" prop="dinner">
            <el-input v-model="form.dinner" placeholder="请输入晚餐" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}" maxlength="250"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="日期" prop="planTime">
            <el-date-picker
              v-model="form.planTime"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="制定人" prop="producer" label-width="80px">
            <el-input v-model="form.producer" placeholder="请输入制定人" />
          </el-form-item>
          <el-form-item label="营养师" prop="dietician">
            <el-input v-model="form.dietician" placeholder="请输入营养师" />
          </el-form-item>
          <el-form-item label="建议" prop="advice">
            <el-input v-model="form.advice" placeholder="请输入建议" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}" maxlength="250" />
          </el-form-item>
        </el-col>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listPlan, getPlan, delPlan, addPlan, updatePlan } from "@/api/business/mealPlan";

  export default {
    name: "Plan",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        planList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          planTime: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          breakFast: [
            { required: true, message: "早餐不能为空", trigger: "blur" }
          ],
          noonFast: [
            { required: true, message: "午餐不能为空", trigger: "blur" }
          ],
          dinner: [
            { required: true, message: "晚餐不能为空", trigger: "blur" }
          ],
          planTime: [
            { required: true, message: "日期不能为空", trigger: "blur" }
          ],
          producer: [
            { required: true, message: "制定人不能为空", trigger: "blur" }
          ],
          dietician: [
            { required: true, message: "营养师不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        listPlan(this.queryParams).then(response => {
          this.planList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          planId: null,
          breakFast: null,
          noonFast: null,
          afternoonTea: null,
          dinner: null,
          producer: null,
          dietician: null,
          advice: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加食谱";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        getPlan(row).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改食谱";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.planId != null) {
              updatePlan(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPlan(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const planIds = row.planId || this.ids;
        this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + planIds + '"的数据项？').then(function() {
          return delPlan(planIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/plan/export', {
          ...this.queryParams
        }, `plan_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style scoped>
.box-card{
  margin: 10px;
  width: 98%;
  height: 600px;
}
.firstCard{
  background-color: #bde6f8;
}
.mealPlan{
  font-size: 10px;
  background-color: #e7f6e9;
  height:150px;
}
.card-container{
  background-color: #77c1fd;
  margin-top:15px;
  border-radius: 10px;
}
/*.time {*/
/*  margin-right: 0px; !* 调整时间标签和按钮之间的距离 *!*/
/*}*/

/*!* 可根据需要调整按钮的样式 *!*/
.button {
  /*margin-left: 0px;*/
  width: 40px;
  /*height: 10px;*/
}
.cscard{
  height: 400px;
  width: 100px;
}
.initCard{
  height:400px;
}
</style>
